package com.mkvld.appietijdenv2.data;

import java.io.Serializable;

import android.content.Context;

import com.mkvld.appietijdenv2.activities.ActMainV9;

public class PeriodBudgetOld implements Serializable {
	private static final long serialVersionUID = 4310511886378394109L;
	
	Budget[] budgets;
	double[] withdrawals;
	int period, year;
	
	public PeriodBudgetOld(Budget vacation, Budget adv, Budget tvt, int period, int year) {
		budgets = new Budget[3];
		budgets[Budget.TYPE_VACATION] = vacation;
		budgets[Budget.TYPE_ADV] = adv;
		budgets[Budget.TYPE_TVT] = tvt;
		this.period = period;
		this.year = year;
	}
	
	public Budget getVacation() {
		return budgets[Budget.TYPE_VACATION];
	}
	
	public Budget getADV() {
		return budgets[Budget.TYPE_ADV];
	}
	
	public Budget getTVT() {
		return budgets[Budget.TYPE_TVT];
	}
	
	public int getPeriod() {
		return period;
	}
	
	public int getYear() {
		return year;
	}
	
	public double[] getWithdrawals() {
		if (withdrawals != null)
			return withdrawals;
		else
			return new double[3];
	}
	
	public double getToBreed(int type, int contract) {
		return budgets[type].getToBreed(period, contract);
	}
	
	public double getTotal(int type, int contract) {
		return budgets[type].getTotal(period, contract);
	}
	
	public double getWithdrawable(int type, int contract) {
		return budgets[type].getWithdrawable(period, contract);
	}
	
	public void next(Context context, int contract) {
		period++;
		if (period > 12) {
			period = 0;
			year++;
		}
		calculateWithdrawals(context, contract);
		for (int i = 0; i < 3; i++) {
			budgets[i].next(period, withdrawals[i], (double)contract - withdrawals[3], contract);
		}
	}

	
	public void calculateWithdrawals(Context context, int contract) {
		withdrawals = new double[]{0,0,0,0};
	//	for (WeekShifts weekShifts : periodShifts) {
	//		withdrawals[Budget.TYPE_ADV] += weekShifts.getSpecialHours(ShiftType.ADV);
	//		withdrawals[Budget.TYPE_VACATION] += weekShifts.getSpecialHours(ShiftType.VACATION);
	//		withdrawals[Budget.TYPE_TVT] += weekShifts.getSpecialHours(ShiftType.TVT);
	//	}
		double withdrawed = 0;
		for (double d : withdrawals) {
			withdrawed += d;
		}
		double worked = ActMainV9.periodShifts.getWorkTime();
		double allowance = ActMainV9.periodShifts.getAllowance().getTotal();
		double total = worked + allowance;
		if (total + withdrawed < contract) {
			double shortage = contract - (total + withdrawed);
			double tvtWithdrawable = budgets[Budget.TYPE_TVT].getWithdrawable(contract, contract) - withdrawals[Budget.TYPE_TVT]; 
			if (tvtWithdrawable > shortage) {
				withdrawals[Budget.TYPE_TVT] += shortage;
			} else {
				if (tvtWithdrawable > 0) {
					withdrawals[Budget.TYPE_TVT] += tvtWithdrawable;
					shortage -= tvtWithdrawable;
				}
				double advWithdrawable = budgets[Budget.TYPE_ADV].getWithdrawable(contract, contract) - withdrawals[Budget.TYPE_ADV];
				if (advWithdrawable > shortage) {
					withdrawals[Budget.TYPE_ADV] += shortage;
				} else {
					if (advWithdrawable > 0) {
						withdrawals[Budget.TYPE_ADV] += advWithdrawable;
						shortage -= advWithdrawable;
					}
					double vacationWithdrawable = budgets[Budget.TYPE_VACATION].getWithdrawable(contract, contract) - withdrawals[Budget.TYPE_VACATION];
					if (vacationWithdrawable > shortage) {
						withdrawals[Budget.TYPE_VACATION] += shortage;
					} else if (vacationWithdrawable > 0)
						withdrawals[Budget.TYPE_VACATION] += vacationWithdrawable; 
				}
			}
			withdrawals[3] = shortage;
		}
	}
	
	@Override
	public String toString() {
		return String.format(Utils.locale, 
				"Periode %d - %d\n\n" +
				"Vacation:\nBudget: %.2f\nBreeded: %.2f\nWithdrawn: %.2f\nNext year: %.2f\n\n" +
				"ADV:\nBudget: %.2f\nBreeded: %.2f\nWithdrawn: %.2f\n\n" +
				"TVT:\nBudget: %.2f\nBreeded: %.2f\nWithdrawn: %.2f", 
				getPeriod() + 1, getYear(), 
				getVacation().getBudget(), getVacation().getBreeded(), 
				getVacation().getWithdrawn(), getVacation().getNextYear(),
				getADV().getBudget(), getADV().getBreeded(), getADV().getWithdrawn(),
				getTVT().getBudget(), getTVT().getBreeded(), getTVT().getWithdrawn());
	}
}
